package down.sallai.ml.controller;

import com.alibaba.fastjson.JSON;
import down.sallai.ml.bean.FileInfo;
import down.sallai.ml.bean.SystemInfo;
import down.sallai.ml.bean.User;
import down.sallai.ml.services.FileManger;
import down.sallai.ml.services.FileMangerImpl;
import down.sallai.ml.services.MonitorService;
import down.sallai.ml.util.Constants;
import down.sallai.ml.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@Controller
@Slf4j
public class Index {
    @Autowired
    FileManger fileManger;

    @Value("${mydown.fileLocal}")
    String fileLocal;

    @Value("${Aname}")
    String userName;

    @Value("${Apwd}")
    String userPassword;

//    @GetMapping({"/index","/"})
//    public String  IndexPage(){
//        return "index";
//    }

    @PostMapping({"/login"})
    @ResponseBody
    public String  Alogin(String Aname,String Apwd,HttpServletRequest request,HttpServletResponse response) throws IOException {
        log.info("登录信息->"+Aname + " "+Apwd);
        if(null!=userPassword&&null!=userName&&userName.equals(Aname)&&userPassword.equals(Apwd)){
            request.getSession().setAttribute("user",new User(userName));
            return new R().setCode(200).setMsg("登陆成功").toString();
        }
        return new R().setCode(300).setMsg("用户名或密码错误").toString();
    }

    @GetMapping("/admin")
    public String admin(Model model,HttpServletRequest request){
//        FileMangerImpl fileManger = new FileMangerImpl();
//        ArrayList<MyFile> allFile = fileManger.getAllFile(fileLocal);
        model.addAttribute("user",request.getSession().getAttribute("user"));
        return "admin/admin";
    }

    @GetMapping("/admin/file")
    public String file(Model model){
        FileMangerImpl fileManger = new FileMangerImpl();
        ArrayList<FileInfo> allFile = fileManger.getAllFile(fileLocal);
        model.addAttribute("fileList",allFile);

        return "admin/file";
    }

    @GetMapping("/admin/systemConfig")
    public String systemConfig(Model model){

        return "admin/systemConfig";
    }

    @GetMapping("/admin/systemInfo")
    public String getSystemInfo(Model model){
        MonitorService monitorService = new MonitorService();
        SystemInfo allSystemInfo = monitorService.getAllSystemInfo(fileLocal);
        model.addAttribute("info", allSystemInfo);
        System.out.println(JSON.toJSONString(allSystemInfo));
        return "admin/systemInfo";
    }

    @PostMapping("/admin/systemInfo_json")
    @ResponseBody
    public String getSystemInfo(){
        MonitorService monitorService = new MonitorService();
        SystemInfo allSystemInfo = monitorService.getAllSystemInfo(fileLocal);
        return JSON.toJSONString(allSystemInfo);
    }

    @GetMapping("/admin/logout")
    public String logout(HttpServletRequest request,HttpServletResponse response){
        request.getSession().removeAttribute("user");
        return "index";
    }

    @GetMapping("/getUserInfo")
    @ResponseBody
    public String  getUserInfo(){
        return "{\"status\":\"0\"}";
    }

    @GetMapping("/admin/server/{state}")
    @ResponseBody
    public String StopServer(@PathVariable("state") String state) {
        if ( "start".equals(state) ) {
            log.info("启动下载服务");
            Constants.Down.DOWN_SERVER_STATE = "true";
        } else if( "stop".equals(state) ){
            log.info("停止下载服务");
            Constants.Down.DOWN_SERVER_STATE = "false";
        } else if ("clear".equals(state)) {
            log.info("清理全部文件");
            fileManger.clearAllFile();
        } else {
            log.info("未知状态，不处理");
        }
        return new R().setMsg("处理完成").setCode(200).toString();
    }

//    @KafkaListener(topics = {"test"})
//    public void onMessage1(ConsumerRecord<?, ?> record){
//        // 消费的哪个topic、partition的消息,打印出消息内容
//        System.out.println("简单消费："+record.topic()+"-"+record.partition()+"-"+record.value());
//    }
}
